package com.concerneyes.glasses.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.glasses.adapter.GlassesColorAdapter;
import com.concerneyes.glasses.adapter.GlassesDetailAdapter;
import com.concerneyes.glasses.model.GlassesDetailModel;
import com.concerneyes.glasses.model.GlassesItemModel;
import com.concerneyes.glasses.task.GlassesImpl;
import com.concerneyes.glasses.task.GlassesListener;

import java.util.HashMap;
import java.util.List;

import common.AbsBaseView;
import http.JsonMap;
import view.GridViewForScrollView;

@SuppressLint("ValidFragment")
public class GlassesDetailView extends AbsBaseView implements GlassesListener, AdapterView.OnItemClickListener {

    private TextView glassesName, jingJia, jingPian, yaJin;
    private GridViewForScrollView glJingPian;
    private GridViewForScrollView glGlassesColor;
    private GlassesImpl impl;
    private GlassesDetailAdapter adapter;
    private GlassesColorAdapter colorAdapter;
    private GlassesDetailModel model;
    private OnImagesListener imagesListener;

    public GlassesDetailView(Context context) {
        super(context);
        imagesListener = (OnImagesListener) context;
        initViews();
    }

    public void initViews() {
        glassesName = (TextView) findViewById(R.id.glasses_name);
        jingJia = (TextView) findViewById(R.id.jingjia);
        jingPian = (TextView) findViewById(R.id.jingpian);
        yaJin = (TextView) findViewById(R.id.yajin);
        glJingPian = (GridViewForScrollView) findViewById(R.id.gl_glasses);
        adapter = new GlassesDetailAdapter(getContext());
        glJingPian.setAdapter(adapter);
        glJingPian.setFocusable(false);
        glJingPian.setOnItemClickListener(this);

        glGlassesColor = (GridViewForScrollView) findViewById(R.id.gl_glasses_color);
        colorAdapter = new GlassesColorAdapter(getContext());
        glGlassesColor.setAdapter(colorAdapter);
        glGlassesColor.setFocusable(false);
        glGlassesColor.setOnItemClickListener(this);
    }

    @Override
    public int layoutResId() {
        return R.layout.fragment_glasses_detail;
    }

    @Override
    public void loadData(Object data) {
        if (data != null) {
            impl = new GlassesImpl(this);
            HashMap<String, String> datas = (HashMap<String, String>) data;
            String productNo = datas.get("productNo");
            String frameColors = datas.get("frameColors");
            impl.glassesInfo(productNo, frameColors);
        }
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    private void setData(GlassesDetailModel datas) {
        model = datas;
        glassesName.setText(datas.getTitle());
        jingJia.setText(datas.getSubtitle());
        jingPian.setText(datas.getSubtitle());
        adapter.clearList();
        adapter.addDatas(datas.getRefractiveList());
        colorAdapter.clearList();
        colorAdapter.addDatas(datas.getColorList());
        String price = datas.getRefractiveList().get(0).getPrice();
        if (!TextUtils.isEmpty(price)) {
            try {
                yaJin.setText("¥" + String.valueOf(Double.parseDouble(datas.getPrice()) + Double.parseDouble(price)));
            } catch (Exception e) {
            }
        } else {
            yaJin.setText("¥" + datas.getPrice());
        }
    }


    @Override
    public void onCategoryListSuccess(List<JsonMap> datas) {

    }

    @Override
    public void onCategoryListFail() {

    }

    @Override
    public void onGlassesListSuccess(GlassesItemModel datas) {

    }

    @Override
    public void onGlassesListFail() {

    }

    @Override
    public void onGlassesInfoSuccess(GlassesDetailModel datas) {
        setData(datas);
    }

    @Override
    public void onGlassesInfoFail() {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.gl_glasses:
                Log.e(this.getClass().getSimpleName(), "position==" + position);
                adapter.setSelection(position);
                String price = adapter.getItem(position).getPrice();
                if (imagesListener != null) {
                    imagesListener.changeRefractive(String.valueOf(adapter.getItem(position).getId()));
                }
                if (!TextUtils.isEmpty(price)) {
                    yaJin.setText("¥" + String.valueOf(Double.valueOf(model.getPrice()) + Double.valueOf(price)));
                }

                break;
            case R.id.gl_glasses_color:
                colorAdapter.setSelection(position);
                String code = colorAdapter.getItem(position).getCode();
                if (imagesListener != null) {
                    imagesListener.changeImages(code);
                }
                break;
        }

    }

    public interface OnImagesListener {
        void changeImages(String code);
        void changeRefractive(String id);
    }

}
